<uieip type="page" value="base"></uieip>
<uieip type="reg" value="console,login,session,cache,http"></uieip>
<!doctype html>
<html>

<head>
    <uieip type="file" value="inc" src="/jsvm/inc/head.html"></uieip>
    <title>登录</title>
</head>

<body>
    <uieip type="file" value="exec" src="/jsvm/inc/title_exec.html"></uieip>
    <div class="container">
        <div class="row" style="margin-top:100px">
            <div class="col-md-offset-4 col-md-4 col-md-offset-3 col-md-5" style="padding: 30px;">
                <div class="panel panel-default">
                    <div class="panel-heading">用户登录</div>
                    <div class="panel-body">
                        <uieip>login_info</uieip>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <uieip type="file" value="inc" src="/jsvm/inc/foot.html"></uieip>
    <script runat="server">
        var backurl_link = "";
        var login_info = "";
        var app_id = "6f443b3811a52d3cc857e34765c71cd5";
        var app_corpsecret = "yq-5053cbe869be75ec54b10047431a5a8e";
        init();

        function init() {
            var backurl = request.param("backurl");
            if (login.state()) {
                if (backurl === null) {
                    response.redirect(request.baseurl());
                } else {
                    response.redirect(backurl);
                }
                return;
            }

            if (!(backurl === null)) {
                session.set("backurl", backurl);
            } else {
                backurl = session.getString("backurl");
                if (backurl === undefined) {
                    backurl = request.clienturl();
                }
            }

            backurl_link = backurl;

            var code = request.param("code");
            if (code === null) {
                response.redirect(code_get());
                return;
            }

            var res = get_token(code);
            if (res.error == 0) {
                //login_info =JSON.stringify(res.data.id_token.payload);
                login_check(res.data.id_token.payload);
            } else {
                login_info = "<p>" + res.info + "[" + res.error + "]</p><a href=\"javascript:clear_get();\">重新登录</a>";
            }
        }

        function code_get() {
            var url = request.clienturl();
            return "http://sso.uieip.com/connect/oidc/authorize?client_id=" + app_id + "&response_type=code&redirect_uri=" + encodeURIComponent(url) + "&scope=snsapi_base&state=STATE&nonce=NONCE";
        }

        function get_token(code) {
            var res = http.post("http://sso.uieip.com/connect/oidc/token?grant_type=authorization_code&code=" + code + "&client_id=" + app_id + "&client_secret=" + app_corpsecret, {
                "Accept": "application/json"
            });

            if (res.error == 0) {
                var res2 = login.idTokenConvert(res.data.id_token,get_public_key());
                if (res2.error == 0) {
                    res.data.id_token = res2;
                    return res;
                } else {
                    return res2;
                }

            } else {
                return res;
            }
        }

        function get_public_key() {
            var res = http.get("http://sso.uieip.com//connect/oidc/.well-known/jwks.json", {
                "Accept": "application/json"
            });

            if (res.error == 0) {
               return res.data.keys[0].n;

            } else {
                return "";
            }
        }

        function login_check(payload) {
            var ck = login.check(payload.sub);
            if (ck.error == 0) {
                backurl = session.getString("backurl");
                if (backurl === undefined) {
                    response.redirect(backurl_link);
                } else {
                    response.redirect(backurl);
                    session.delete("backurl");
                }
            } else if (ck.error == 2) {  //用户在本地不存在
                login_info = "<p>登录用户：" + payload.login_name + " 不存在</p><a href=\"javascript:clear_get();\">重新登录</a>";
            } else {
                login_info = "<p>" + ck.info + "[" + ck.error + "]</p><a href=\"javascript:clear_get();\">重新登录</a>";
            }
        }
    </script>
    <script>
        var backurl = "<uieip>backurl_link</uieip>";

        function clear_get() {
            location = "http://sso.uieip.com/login/logout.html?backurl=" + encodeURIComponent(backurl_get());
        }

        function backurl_get() {
            const url = new URL(location.href.replace(/#$/, ''));
            const _backurl = url.searchParams.get("backurl");
            return _backurl || backurl;
        }
    </script>
</body>

</html>